package com.example.it.studymenu;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.RelativeLayout;

public class ContextMenuActivity extends AppCompatActivity {

    private RelativeLayout _rLayout;

    final private int BLUE = 1;
    final private int RED = 2;
    final private int GREEN = 3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_context_menu);

        //Step 1: 为 View 注册contextMenu
        _rLayout = (RelativeLayout) findViewById(R.id.activity_context_menu);
        this.registerForContextMenu(_rLayout);
    }

    /*
    * Step 2 ：为创建contentMenu
    * */
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        //创建Menu的方式和是OptaionsMenu 的方式相同
        menu.add(Menu.NONE, this.BLUE, Menu.NONE, "BLUE");
        menu.add(Menu.NONE, this.RED, Menu.NONE, "RED");
        menu.add(Menu.NONE, this.GREEN, Menu.NONE, "GREEN");
    }

    /*
    * Step 3 : 添加监听事件
    * */
    @Override
    public boolean onContextItemSelected(MenuItem item) {

        int index = item.getItemId();
        switch (index) {
            case BLUE:
                this._rLayout.setBackgroundColor(Color.BLUE);
                break;
            case RED:
                this._rLayout.setBackgroundColor(Color.RED);
                break;
            case GREEN:
                this._rLayout.setBackgroundColor(Color.GREEN);
                break;
            default:
                break;
        }
        
        return super.onContextItemSelected(item);
    }
}
